package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.GradeBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class ResultRankingAdapter extends RecyclerView.Adapter<ResultRankingAdapter.ActivityHolder> {

    private Context context;
    private List<GradeBean.PageBean.ListBean> mData;


    public ResultRankingAdapter(Context context, List<GradeBean.PageBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_track_result_ranking, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {
            GradeBean.PageBean.ListBean listBean = mData.get(position);
            String name = listBean.getName().toString().trim();
            String mileage = listBean.getTotalMileage();
            int taskNum = listBean.getTaskNum();
            int clockNum = listBean.getClockNum();

            holder.tvNum.setText((position + 1) + "");


            if (position == 0) {
                holder.tvNum.setBackground(context.getDrawable(R.drawable.shape_circle_green));
            } else if (position == 1) {
                holder.tvNum.setBackground(context.getDrawable(R.drawable.shape_circle_ffc));
            } else if (position == 2) {
                holder.tvNum.setBackground(context.getDrawable(R.drawable.shape_circle_red));
            } else {
                holder.tvNum.setBackgroundColor(context.getResources().getColor(R.color.transparent));
                holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color333));
            }


            if (!StringUtils.isEmpty(name)) {
                holder.tvName.setText(name);
            }

            if (!StringUtils.isEmpty(mileage)) {
                holder.tvMileage.setText(mileage + "km");
            }

            holder.tvDay.setText(taskNum + "天");
            holder.tvSignInNum.setText(clockNum + "次");
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(List<GradeBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }


    public static class ActivityHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_mileage)
        TextView tvMileage;
        @BindView(R.id.tv_day)
        TextView tvDay;
        @BindView(R.id.tv_sign_in_num)
        TextView tvSignInNum;
        @BindView(R.id.layout)
        RelativeLayout layout;

        public ActivityHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
